package com.coderush.admin.core.interceptors;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * PackageName com.coderush.admin.core.interceptors
 *
 * @Author Eason
 * @Version v1.0
 * @ClassName RequestInterceptor
 * @Date 2025/9/2
 * @Description 请求拦截器
 *              获取客户端的访问信息
 */
@Slf4j
@Component
public class RequestInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 在控制器执行前获取客户端信息
        String clientIp = getClientIp(request);
        String requestUrl = request.getRequestURL().toString();
        String method = request.getMethod();
        String userAgent = request.getHeader("User-Agent");

        request.setAttribute("startTime", System.currentTimeMillis()); // 记录请求开始时间
        request.setAttribute("clientIp", clientIp); // 添加客户端IP
        request.setAttribute("requestUrl", requestUrl); //  添加请求URL
        request.setAttribute("method", method); // 添加请求方法
        request.setAttribute("userAgent", userAgent); // 添加用户代理信息

        // 打印或记录日志

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 在控制器执行完成后执行
        // 可以在此处进行清理工作，如移除线程绑定的变量等

        Long startTime = (Long) request.getAttribute("startTime"); // 获取请求开始时间
        long duration = System.currentTimeMillis() - startTime; // 计算请求耗时
        String clientIp = (String) request.getAttribute("clientIp"); // 获取客户端IP
        String requestUrl = (String) request.getAttribute("requestUrl"); // 获取请求URL
        String method = (String) request.getAttribute("method"); // 获取请求方法
        String userAgent = (String) request.getAttribute("userAgent"); // 获取用户代理信息

        log.info("请求拦截器完成 - IP: {}, URL: {}, Method: {}, User-Agent: {}, 请求耗时: {}ms ", clientIp, requestUrl, method, userAgent, duration);
    }


    // 获取客户端真实IP地址
    private String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

}